package com.sam.gui.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.sam.dejian.PlatformPreference;
import com.sam.gui.base.BaseWebActivity;
import com.sam.platmform.unimplements.PlatformBRHelper;
import com.sam.utils.pkg.ApkDownloadUtils;
import com.sam.utils.pkg.ApplicationUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015-10-27.
 */
public class WebLoginWebActivity extends BaseWebActivity {
    private String auto_login_checkbox;

    @Override
    protected void initView() {
        super.initView();
        setContentUrl("index.php?c=user&a=login&platform=android&version=" + ApplicationUtils.getVersionCode(this));
        setPageFinishedListener(new PageFinishedListener() {
            @Override
            public void onPageFinished(WebView view) {
                String lastUserName = PlatformPreference.getInstance(WebLoginWebActivity.this).getAccount(null);
                String lastUserPasswd = PlatformPreference.getInstance(WebLoginWebActivity.this).getPassword("");
                if (lastUserName != null) {
                    view.loadUrl(String.format("javascript:set_auto('" + lastUserName + "','" + lastUserPasswd + "')"));
                    auto_login_checkbox = "1";
                }
            }
        });
    }

    @JavascriptInterface
    public void loginSuccess(String id, String role, String username, String password, String token, String auto_login) {
        if (role.equals("user")) {    //用户身份
            PlatformPreference.getInstance(this).setAccount(username);
            PlatformPreference.getInstance(this).setPassword(password);
            PlatformBRHelper.getInstance().sendLoginSecceededBR(this, username, password, token, id);
            Intent intent = new Intent(this, MainHomeWebActivity.class);
            startActivity(intent);
            finish();
        } else {  //医生身份
            alert("其他用户身份暂不支持!");
        }
    }

    @JavascriptInterface
    public void get_pwd() {
        Intent intent = new Intent(this, ResetPwdWebActivity.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void regPage(String type) {
        Intent intent = new Intent(this, RegisterWebActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
    }


    @JavascriptInterface
    public void new_yy() {  //新预约
        Intent intent = new Intent(this, MakeAppointmentWebActivity.class);
        startActivity(intent);
    }

    @JavascriptInterface
    public void update_soft(String url, String version, String log) {
        ApkDownloadUtils updateApk = new ApkDownloadUtils(this, url, "djw_user/apk");
        updateApk.showUpdateDialog();
    }

    @JavascriptInterface
    public void setIs_auto_login(String a) {
        auto_login_checkbox = a;
    }

}
